﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Condimar.Entities;

namespace Condimar.Repository
{
    public class ClienteDAO:MasterRepository, IClienteDAO
    {

        public List<Cliente> GetAllFromClientes()
        {
            var query = from c in context.Clientes
                        select c;
            return query.ToList();
        }

        public List<Cliente> GetFromClienteByNatural(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in context.Clientes
                            //where c.Nombre.ToUpper().Contains(descripcion.ToUpper()) ||
                            //c.Dni.Contains(descripcion)
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in context.Clientes
                             //where (c.Dni).Length.Equals(8)
                             select c;
                return query1.ToList();
            }
        }

        public List<Cliente> GetFromClienteByJuridoco(string descripcion)
        {
            //if (descripcion != "")
            //{
            //    //var query = from c in context.Clientes
            //    //            where c.RazonSocial.ToUpper().Contains(descripcion.ToUpper()) ||
            //    //            c.Ruc.Contains(descripcion)
            //    //            select c;
            //    //return query.ToList();
            //}
            //else
            //{
                var query1 = from c in context.Clientes
                             //where (c.Ruc).Length.Equals(11)
                             select c;
                return query1.ToList();
            //}
        }

        public Cliente GetFromClienteById(int id)
        {
            return context.Clientes.Find(id);
        }

        public void AddCliente(Cliente cliente)
        {
            context.Clientes.Add(cliente);
            context.SaveChanges();
        }

        public void UpdateCliente(Cliente cliente)
        {
            context.Entry(cliente).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveCliente(int id)
        {
            var existe = from c in context.Clientes
                         where c.Id.Equals(id)
                         select c;

            context.Clientes.Remove(existe.SingleOrDefault());
            context.SaveChanges();
        }
    }
}
